/*
 * Properties.cpp
 *
 *  Created on: Aug 26, 2011
 *      Author: mengwei.chua
 */

#include "Properties.h"
#include "ParserHelper.h"

namespace projectcos
{
namespace tile
{
namespace parser
{

Properties::Properties()
{
	m_PropertyList.clear();
}

Properties::~Properties()
{
	m_PropertyList.clear();
}

bool Properties::Parse(const TiXmlElement* element)
{
	if(NULL != element)
	{
		std::string name;
		std::string value;

		const TiXmlElement* child = element->FirstChildElement("property");

		while(NULL != child)
		{
			TIXML_QUERY_STRING(child, "name", name);
			TIXML_QUERY_STRING(child, "value", value);

			// If it is non-empty name
			if(!name.empty())
			{
				m_PropertyList[name] = value;
			}

			child = child->NextSiblingElement("property");
		}
	}
	else
	{
		return false;
	}

	return true;
}

void Properties::GetPropertyList(std::deque<Property>& propertyList) const
{
	propertyList.clear();

	PropertyMap::const_iterator it = m_PropertyList.begin();
	PropertyMap::const_iterator endIt = m_PropertyList.end();

	while(it != endIt)
	{
		propertyList.push_back(Property(it->first, it->second));
		it++;
	}
}

std::string Properties::GetValue(const std::string& name) const
{
	PropertyMap::const_iterator it = m_PropertyList.find(name);
	if(m_PropertyList.end() != it)
	{
		return it->second;
	}

	return "";
}

} // parser
} // tile
} // projectcos
